home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / rad64.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-05  |  1.7 KB  |  115 lines

  1. /*
  2.  * Copyright 1989, 1990, 1992, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #include "config.h"
  13.  
  14. #ifndef    lint
  15. static    char    sccsid[] = "@(#)rad64.c    3.3    20:38:01    3/7/92";
  16. #endif
  17.  
  18. /*
  19.  * c64i - convert a radix 64 character to an integer
  20.  */
  21.  
  22. int    c64i (c)
  23. char    c;
  24. {
  25.     if (c == '.')
  26.         return (0);
  27.  
  28.     if (c == '/')
  29.         return (1);
  30.  
  31.     if (c >= '0' && c <= '9')
  32.         return (c - '0' + 2);
  33.  
  34.     if (c >= 'A' && c <= 'Z')
  35.         return (c - 'A' + 12);
  36.  
  37.     if (c >= 'a' && c <= 'z')
  38.         return (c - 'a' + 38);
  39.     else
  40.         return (-1);
  41. }
  42.  
  43. /*
  44.  * i64c - convert an integer to a radix 64 character
  45.  */
  46.  
  47. int    i64c (i)
  48. int    i;
  49. {
  50.     if (i < 0)
  51.         return ('.');
  52.     else if (i > 63)
  53.         return ('z');
  54.  
  55.     if (i == 0)
  56.         return ('.');
  57.  
  58.     if (i == 1)
  59.         return ('/');
  60.  
  61.     if (i >= 2 && i <= 11)
  62.         return ('0' - 2 + i);
  63.  
  64.     if (i >= 12 && i <= 37)
  65.         return ('A' - 12 + i);
  66.  
  67.     if (i >= 38 && i <= 63)
  68.         return ('a' - 38 + i);
  69.  
  70.     return ('\0');
  71. }
  72.  
  73. #ifdef NEED_AL64
  74.  
  75. /*
  76.  * l64a - convert a long to a string of radix 64 characters
  77.  */
  78.  
  79. char    *l64a (l)
  80. long    l;
  81. {
  82.     static    char    buf[8];
  83.     int    i = 0;
  84.  
  85.     if (i < 0L)
  86.         return ((char *) 0);
  87.  
  88.     do {
  89.         buf[i++] = i64c ((int) (l % 64));
  90.         buf[i] = '\0';
  91.     } while (l /= 64L, l > 0 && i < 6);
  92.  
  93.     return (buf);
  94. }
  95.  
  96. /*
  97.  * a64l - convert a radix 64 string to a long integer
  98.  */
  99.  
  100. long    a64l (s)
  101. char    *s;
  102. {
  103.     int    i;
  104.     long    value;
  105.     long    shift = 0;
  106.  
  107.     for (i = 0, value = 0L;i < 6 && *s;s++) {
  108.         value += (c64i (*s) << shift);
  109.         shift += 6;
  110.     }
  111.     return (value);
  112. }
  113.  
  114. #endif /* NEED_A64L */
  115.